package com.nttuan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nttuan.model.entity.GridTask;
import com.nttuan.mycalender.R;
import com.nttuan.util.Support;

import java.util.Date;
import java.util.List;

/**
 * Created by TuanNT on 12/21/2014.
 */
public class GridAdapter extends BaseAdapter{
    private Context context;
    private LayoutInflater inflater;
    private List<GridTask> list;
    private String[] arrDayOfWeek;
    public GridAdapter(Context context, List<GridTask> list) {
        this.context = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
        this.arrDayOfWeek = context.getResources().getStringArray(R.array.dayOfWeek);
        this.arrDayOfWeek[0] = "C Nhật";
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHoller holler;
        if(view == null){
            holler = new ViewHoller();
            view = inflater.inflate(R.layout.item_grid_coming, null);
            holler.dateOfWeek =(TextView)view.findViewById(R.id.textDayGridItem);
            holler.date =(TextView)view.findViewById(R.id.textDateGridItem);
            holler.numOfTask =(TextView)view.findViewById(R.id.textNumOfTaskGridItem);
            holler.layout =(RelativeLayout)view.findViewById(R.id.rLayoutGridItem);
            view.setTag(holler);
        }else
            holler = (ViewHoller)view.getTag();
        GridTask gridTask = list.get(i);
        holler.dateOfWeek.setText(arrDayOfWeek[Support.dateOfWeek(gridTask.getDate())]);
        holler.date.setText(displayDate(gridTask.getDate()));
        holler.numOfTask.setText(gridTask.getNumOfTask()+"");

        return view;
    }
    private String displayDate(Date date){
        return date.getDate() +" / " + date.getMonth();
    }
    class ViewHoller{
        TextView dateOfWeek;
        TextView date;
        TextView numOfTask;
        RelativeLayout layout;
    }
}
